package com.lagou.core;

import org.apache.catalina.LifecycleException;
import org.apache.catalina.connector.Connector;
import org.apache.catalina.startup.Tomcat;

import javax.servlet.ServletException;
import java.io.File;
import java.io.IOException;

/**
 * @author: jie.zhao
 * @create: 2021/03/16 18:10
 */
public class TomcatServer {

    public static final String DEFAULT_PROTOCOL = "org.apache.coyote.http11.Http11NioProtocol";

    public TomcatServer(String port) {
        this.port = port;
        init();
    }

    private void init() {
        Tomcat tomcat = new Tomcat();

        try {
            tomcat.addWebapp("/","/Users/duodiankeji/Downloads/");
            tomcat.setPort(Integer.parseInt(port));
            tomcat.start();
            tomcat.getServer().await();
        } catch (LifecycleException e) {
            e.printStackTrace();
        }

    }

    private File createTempDir(String prefix) {
        try {
            File tempDir = File.createTempFile(prefix + ".", "." + getPort());
            tempDir.delete();
            tempDir.mkdir();
            tempDir.deleteOnExit();
            return tempDir;
        }
        catch (IOException ex) {
            ex.printStackTrace();
        }
        return null;
    }

    private String port;

    public String getPort() {
        return port;
    }

    public void setPort(String port) {
        this.port = port;
    }
}
